<?php

namespace App\Console\Commands\Cruise;

use App\Models\Cruise\CruiseCheckSelfDetailModel;
use App\Models\Cruise\CruiseCheckSelfTaskModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class GenerateSelfCheckTaskCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cruise:generateSelfCheckTaskCommand';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '稽核 - 自检任务生成';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        CruiseCheckSelfTaskModel::query()
                                ->with(["cruiseCheckSelfTaskCompanyMappingsRel"])
                                ->where("status", CruiseCheckSelfTaskModel::STATUS_ON)
                                ->chunkById(100, function (Collection $CruiseCheckSelfTaskModelCollection) {
                                    $CruiseCheckSelfTaskModelCollection->each(function (
                                        CruiseCheckSelfTaskModel $cruiseCheckSelfTaskModel
                                    ) {
                                        if (
                                            $cruiseCheckSelfTaskModel->taskStartTime <= time() && $cruiseCheckSelfTaskModel->taskEndTime + 24 * 60 * 60 > time()
                                        ) {
                                            CruiseCheckSelfDetailModel::query()->insert(
                                                $cruiseCheckSelfTaskModel
                                                    ->cruiseCheckSelfTaskCompanyMappingsRel
                                                    ->map(
                                                        function ($v) {
                                                            return [
                                                                'shopId'               => $v->shopId,
                                                                "cruiseCheckCompanyId" => $v->cruiseCheckCompanyId,
                                                                "taskId"               => $v->taskId,
                                                                "date"                 => date("Ymd", time()),
                                                                "status"               => CruiseCheckSelfDetailModel::STATUS_WAIT,
                                                                "createdAt"            => time()
                                                            ];
                                                        }
                                                    )->values()->toArray());
                                        }
                                    });
                                });

        return 0;
    }
}
